package pers.zhouyoudianxi.notice2.serviceImpl;

import pers.zhouyoudianxi.notice2.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import pers.zhouyoudianxi.notice2.view.MainFrame;

import javax.swing.*;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Service
public class NoticeServiceImpl implements NoticeService {

    @Override
    public boolean send(String title, String detail) {
        log.info("即将弹窗……");
        // 测试方法
        try {
            MainFrame.showTrayMessage(title, detail, TrayIcon.MessageType.INFO);
            // 单线程直接调用OK   定时任务执行失败
            SwingUtilities.invokeLater(() -> {
                JDialog dialog = new JDialog();
                dialog.setTitle(title);
                dialog.setModal(true); // 模态窗口，阻止其他操作
                dialog.setSize(300, 200);
                dialog.setLocationRelativeTo(null); // 居中显示
                // 设置对话框内容
                JPanel panel = new JPanel(new BorderLayout(10, 10));
                panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

                // 显示当前时间
                String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                JLabel timeLabel = new JLabel("当前时间: " + time);
                timeLabel.setHorizontalAlignment(SwingConstants.CENTER);

                // 提示信息
                JLabel messageLabel = new JLabel(detail, SwingConstants.CENTER);

                // 确认按钮
                JButton confirmBtn = new JButton("确认");
                confirmBtn.addActionListener(e -> dialog.dispose()); // 关闭对话框
                JPanel btnPanel = new JPanel();
                btnPanel.add(confirmBtn);

                // 组装面板
                panel.add(timeLabel, BorderLayout.NORTH);
                panel.add(messageLabel, BorderLayout.CENTER);
                panel.add(btnPanel, BorderLayout.SOUTH);

                dialog.getContentPane().add(panel);
                dialog.setVisible(true);
            });

            log.info("弹窗成功");
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        log.info("弹窗失败");
        return false;
    }
}
